package com.team5101.controller;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.team5101.entity.VO.SeatInfoVO;
import com.team5101.service.ISeatService;
import com.team5101.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/*
        关于座位后台的处理
        1.根据厅号Hall_id,放映id tid，座位状态stateType    查询 当前已被需求（已买票/）的的座位号
           接口 : /api/seat/querySeatById/{tid}/{stateType}/{Hall_id}
 */

@Controller
@RequestMapping("/api/seat/")
public class SeatController {

    @Autowired
    private ISeatService iSeatService;

    /*
     *  说明 : 根据厅号,放映id，座位状态       查询 当前已被购买的的座位号    座位状态stateType
     *
     *  接口 : /api/seat/querySeatById/{tid}/{stateType}/{Hall_id}
     *
     *  参数 : 厅号-hallId  放映id   tId
     */
    @RequestMapping("/querySeatById/{tid}/{stateType}/{hall_id}")
    @ResponseBody
    public R querySeatById(@PathVariable("tid") Integer tId,@PathVariable Integer stateType,@PathVariable("hall_id") Integer hallId){
        //Integer n = iSeatService.queryNumByhallId(hallId);
        return R.ok(iSeatService.queryBuySeatByID(tId, hallId,stateType));
    }


    /*
     *  说明 : 根据厅号,放映id，座位状态       查询 当前已被购买的的座位号    座位状态stateType
     *
     *  接口 : /api/seat/addseats
     *
     *  参数 : seatInfoVO 包含座位数组 用户id
     */
    @RequestMapping("/addseats")
    @ResponseBody
    public R InsertSeatByHallID(@RequestBody SeatInfoVO seatInfoVO){
        /*从前端获取的 参数： 订单详情数据 */
        String  data=seatInfoVO.getNums();
        //后端的jackson  进行转换： 将 String ====》List<OrderCourse>
        ObjectMapper objectMapper = new ObjectMapper();
        //json转list<User>
        JavaType jt = objectMapper.getTypeFactory().constructParametricType(ArrayList.class, Integer.class);
        List<Integer> nums=null;
        try {
             nums = objectMapper.readValue(data, jt);
             if(seatInfoVO.getNumSeat()==null)
             seatInfoVO.setNumSeat(nums);
        } catch (IOException e) {
            e.printStackTrace();
            return R.error(10000,"订单详情信息不正确！");
        }
//        System.out.println(seatInfoVO);
        return R.ok(iSeatService.InsertSeatByHallID(seatInfoVO));
    }


    /*
     *  说明 : 根据厅号,放映id，座位状态       修改 当前座位状态stateType
     *
     *  接口 : /api/seat/updateseats
     *
     *  参数 : seatInfoVO 包含座位数组 用户id
     */
    @RequestMapping("/updateseats")
    @ResponseBody
    public R updateSeatState(@RequestBody SeatInfoVO seatInfoVO){
        /*从前端获取的 参数： 订单详情数据 */

        String  data=seatInfoVO.getNums();

        //后端的jackson  进行转换： 将 String ====》List<OrderCourse>
        ObjectMapper objectMapper = new ObjectMapper();
        //json转list<User>
        JavaType jt = objectMapper.getTypeFactory().constructParametricType(ArrayList.class, Integer.class);

        List<Integer> nums=null;
        try {
            nums = objectMapper.readValue(data, jt);
            if(seatInfoVO.getNumSeat()==null)
                seatInfoVO.setNumSeat(nums);
        } catch (IOException e) {
            e.printStackTrace();
            return R.error(10000,"订单详情信息不正确！");
        }
//        System.out.println(seatInfoVO);
        return R.ok(iSeatService.UpdataSeatState(seatInfoVO));
    }

    /*
     *  说明 : 根据信息删除座位
     *
     *  接口 : /api/seat/deleteseats
     *
     *  参数 : seatInfoVO 包含座位数组 用户id
     */
    @RequestMapping("/deleteseats")
    @ResponseBody
    public R deleteSeat(@RequestBody SeatInfoVO seatInfoVO){
        String  data=seatInfoVO.getNums();
        //后端的jackson  进行转换： 将 String ====》List<OrderCourse>
        ObjectMapper objectMapper = new ObjectMapper();
        //json转list<User>
        JavaType jt = objectMapper.getTypeFactory().constructParametricType(ArrayList.class, Integer.class);
        List<Integer> nums=null;
        try {
            nums = objectMapper.readValue(data, jt);
            if(seatInfoVO.getNumSeat()==null)
                seatInfoVO.setNumSeat(nums);
        } catch (IOException e) {
            e.printStackTrace();
            return R.error(10000,"订单详情信息不正确！");
        }
        System.out.println(seatInfoVO);
        return R.ok(iSeatService.deleteSeat(seatInfoVO));
    }

}
